package com.example.demo.controller;

import com.example.demo.dto.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Health Check Controller
 * 
 * Provides health check and system information endpoints
 */
@RestController
@RequestMapping("/api")
@Slf4j
public class HealthController {

    /**
     * Health check endpoint
     * 
     * @return Health status
     */
    @GetMapping("/health")
    public ResponseEntity<ApiResponse<Map<String, Object>>> health() {
        log.debug("Health check endpoint called");
        
        Map<String, Object> health = new HashMap<>();
        health.put("status", "UP");
        health.put("service", "springboot-apm-demo");
        health.put("version", "1.0.0");
        health.put("timestamp", System.currentTimeMillis());
        
        return ResponseEntity.ok(ApiResponse.success("Service is healthy", health));
    }

    /**
     * Welcome endpoint
     * 
     * @return Welcome message
     */
    @GetMapping("/welcome")
    public ResponseEntity<ApiResponse<Map<String, String>>> welcome() {
        log.info("Welcome endpoint called");
        
        Map<String, String> info = new HashMap<>();
        info.put("message", "Welcome to Spring Boot APM Demo");
        info.put("description", "This application demonstrates integration with SkyWalking and Sonar");
        info.put("documentation", "/docs/README.md");
        
        return ResponseEntity.ok(ApiResponse.success("Welcome!", info));
    }

    /**
     * Simulate slow endpoint for testing APM
     * 
     * @return Response after delay
     */
    @GetMapping("/slow")
    public ResponseEntity<ApiResponse<Map<String, Object>>> slowEndpoint() {
        log.info("Slow endpoint called - simulating delay");
        
        try {
            // Simulate slow operation (1-3 seconds)
            int delay = 1000 + (int) (Math.random() * 2000);
            Thread.sleep(delay);
            
            Map<String, Object> result = new HashMap<>();
            result.put("message", "Slow operation completed");
            result.put("delayMs", delay);
            
            return ResponseEntity.ok(ApiResponse.success("Operation completed", result));
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            log.error("Slow endpoint interrupted", e);
            return ResponseEntity.internalServerError()
                    .body(ApiResponse.error("Operation interrupted"));
        }
    }

    /**
     * Simulate error endpoint for testing APM error tracking
     * 
     * @return Error response
     */
    @GetMapping("/error")
    public ResponseEntity<ApiResponse<Void>> errorEndpoint() {
        log.error("Error endpoint called - simulating error");
        throw new RuntimeException("This is a simulated error for testing APM");
    }
}

